package service

import (
	"goHttp/config"
	"log"
	"os"
	"strings"
	"time"
)

func CreateDir() (resultPath, errPath string) {
	resultPath = config.C.Log.ResultPath
	errPath = config.C.Log.ErrDetailPath
	dir, err := os.Getwd()
	if err != nil {
		panic("获取所在路径失败：" + err.Error())
	}
	//检测是否配置了日志路径
	if len(config.C.Log.ResultPath) == 0 {
		resultPath = dir + "\\res_log\\"
	}
	if len(config.C.Log.ErrDetailPath) == 0 {
		errPath = dir + "\\err_log\\"
	}

	_, err = os.Stat(resultPath)
	if err != nil {
		if !os.IsNotExist(err) {
			panic("获取日志目录失败：" + err.Error())
		}
		err = os.Mkdir(resultPath, os.ModePerm)
		if err != nil {
			panic("创建目录失败：" + err.Error())
		}
	}
	_, err = os.Stat(errPath)
	if err != nil {
		if !os.IsNotExist(err) {
			panic("获取日志目录失败：" + err.Error())
		}
		err = os.Mkdir(errPath, os.ModePerm)
		if err != nil {
			panic("创建目录失败：" + err.Error())
		}
	}
	return
}

func RecordErr(urls []string, newStartTime, newEndTime time.Time, body []byte, errPath string) {

	err := os.WriteFile(errPath+"\\"+urls[len(urls)-1]+"【"+newStartTime.Format("2006-01-02-15-04-05")+"~"+newEndTime.Format("2006-01-02-15-04-05")+"】"+".json", body, os.ModePerm)
	if err != nil {
		panic("写入文件失败" + err.Error())
	}
	log.Println(newStartTime.Format(time.DateTime), newEndTime.Format(time.DateTime), "【失败】", strings.Join(urls, "/"))
}
